package com.meme.editor.gui.sprite2d;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

import org.apache.log4j.Logger;

import com.meme.app.AppRegistry;
import com.meme.editor.EntityObject;
import com.meme.editor.gui.EntityChildrenListView;
import com.meme.editor.gui.PropertyEditPanel;
import com.meme.editor.model.sprite2d.CompositedModuleGroup;
import com.meme.editor.model.sprite2d.ModuleGroup;
import com.meme.editor.model.sprite2d.PureModuleGroup;
import com.meme.editor.model.sprite2d.Tiled2dPalette;
import com.meme.editor.model.sprite2d.Sprite2dFile;
import com.meme.editor.model.sprite2d.Sprite2dFile.CompositedModuleGroupSet;
import com.meme.editor.model.sprite2d.Sprite2dFile.PureModuleGroupSet;

public class Tiled2dPaletteContentPanel extends JPanel
{
	static Logger logger = Logger.getLogger (Tiled2dPaletteContentPanel.class);
	

	JSplitPane mSplitPane_ChildrenList;
	JSplitPane mSplitPane_View;
	JSplitPane mSplitPane_Main;
	JTabbedPane mTabbedPanel;
	
	public Tiled2dPaletteContentPanel (Tiled2dPalette tiled2dPalette)
	{
		int width  = ( (Integer) AppRegistry.getRegistry("editor.sprite2d.contentPanel.width") ).intValue();
		int height = ( (Integer) AppRegistry.getRegistry("editor.sprite2d.contentPanel.height") ).intValue();

		mTabbedPanel = new JTabbedPane ();
		//mTabbedPanel.setTabPlacement(JTabbedPane.BOTTOM);
		PureModuleGroupSet pureModuleGroupSet = (PureModuleGroupSet) ( ( (Sprite2dFile) ( tiled2dPalette.getHost() ) ).getPureModuleGroupSet() );	
		for (EntityObject group : pureModuleGroupSet.getChildren())
		{
			ModuleGroup moduleGroup = (ModuleGroup)group;			
			mTabbedPanel.add(moduleGroup.toString(), new ModuleGroupPanel ( moduleGroup, ModuleGroupPanel.ViewMode_Bird, ModuleGroupPanel.EditMode_CompositedModule, moduleGroup instanceof PureModuleGroup) );
		}
		CompositedModuleGroupSet compositedModuleGroupSet = (CompositedModuleGroupSet) ( ( (Sprite2dFile) ( tiled2dPalette.getHost() ) ).getCompositedModuleGroupSet() );	
		for (EntityObject group : compositedModuleGroupSet.getChildren())
		{
			ModuleGroup moduleGroup = (ModuleGroup)group;			
			mTabbedPanel.add(moduleGroup.toString(), new ModuleGroupPanel ( moduleGroup, ModuleGroupPanel.ViewMode_Icons, ModuleGroupPanel.EditMode_CompositedModule, moduleGroup instanceof PureModuleGroup) );
		}
		
		
		Tiled2dPaletteView tiled2dPaletteView = new Tiled2dPaletteView (tiled2dPalette);
		PropertyEditPanel modulePropertyPanel = new PropertyEditPanel ();
		tiled2dPaletteView.setCellPropertyEditPanel (modulePropertyPanel);
		
		EntityChildrenListView childrenView = new EntityChildrenListView (tiled2dPalette, false, false, true);
		childrenView.setPropertyEditPanel (modulePropertyPanel);		

		mSplitPane_ChildrenList = new JSplitPane (JSplitPane.VERTICAL_SPLIT, 
				modulePropertyPanel, 
				childrenView 
                );
		mSplitPane_ChildrenList.setResizeWeight (0.0);
		mSplitPane_ChildrenList.setDividerSize  (2);
		mSplitPane_ChildrenList.setDividerLocation (200);
		
		mSplitPane_View = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, 
				mSplitPane_ChildrenList, 
				tiled2dPaletteView 
                );
		mSplitPane_View.setResizeWeight (0.0);
		mSplitPane_View.setDividerSize  (2);
		mSplitPane_View.setDividerLocation (width * 1 / 5);
		

		mSplitPane_Main = new JSplitPane (JSplitPane.VERTICAL_SPLIT, 
				mSplitPane_View, 
				mTabbedPanel 
                );
		mSplitPane_Main.setResizeWeight (1.0);
		mSplitPane_Main.setDividerSize  (2);
		mSplitPane_Main.setDividerLocation (height * 2 / 3);
		
		
//		JSplitPane mSplitPane_Bottom = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, 
//				modulePropertyPanel, 
//				mTabbedPanel
//		        );
//		mSplitPane_Bottom.setResizeWeight (0.0);
//		mSplitPane_Bottom.setDividerSize  (2);
//		mSplitPane_Bottom.setDividerLocation (width * 1 / 5);
//		
//		
//		mSplitPane_Main = new JSplitPane (JSplitPane.VERTICAL_SPLIT, 
//				tiled2dPaletteView, 
//				mSplitPane_Bottom 
//		        );
//		mSplitPane_Main.setResizeWeight (1.0);
//		mSplitPane_Main.setDividerSize  (2);
//		mSplitPane_Main.setDividerLocation (width * 2 / 3);
		
		setLayout ( new BorderLayout() );
		add (mSplitPane_Main, BorderLayout.CENTER);		
	}
}
